/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.config;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.config.ProfilesConfig;
import opiam.admin.faare.config.javabeans.JBProfiles;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;


/**
 *  Tests the loading of the Profiles configuration.
 */
public class ProfilesConfigTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(ProfilesConfigTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /**
     * Creates a new ProfilesConfigTest object.
     *
     * @param param  Parameter.
     */
    public ProfilesConfigTest(String param)
    {
        super(param);
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new ProfilesConfigTest("testBegin"));

        // TESTS SET
        suite.addTest(new ProfilesConfigTest("testReadConfig"));

        suite.addTest(new ProfilesConfigTest("testEnd"));

        return (suite);
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("ProfilesConfigTest");

        _test.displayHeaderClass("ProfilesConfigTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("ProfilesConfigTest");
    }

    /**
     * Tests the readConfig method.
     *
     */
    public void testReadConfig()
    {
        _test.displayHeaderMethod("testReadConfig");

        try
        {
            JBProfiles jbProfiles = ProfilesConfig.getInstance()
                                                               .readConfig(this.getClass()
                                     .getResourceAsStream(PropertiesManager.PROFILES_XML_FILE));

            if (jbProfiles != null)
            {
                _test.displayMsg(jbProfiles.toString());
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure("No configuration");
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testReadConfig");
    }
}
